package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SigninMessage;
import com.tianji.learning.service.ISignRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@Component
public class LearningPointsListener {

    private final ISignRecordService recordService;

    /**
     * 监听添加登录积分记录
     * @param message
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "sign.points.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN
    )})
    public void listenSignInMessage(SigninMessage message){
        recordService.addPointsRecord(message.getUserId(),message.getPoints(), PointsRecordType.SIGN);
    }

    /**
     * 监听添加回答问题积分记录
     * @param userId
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name="qa.points.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY
    )})
    public void listenWriteReplyMessage(Long userId){
        recordService.addPointsRecord(userId,5,PointsRecordType.QA);
    }

    /**
     * 监听每学习一小节增加学习记录
     * @param userId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "learning.points.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.LEARN_SECTION
    ))
    public void listenLearningReplyMessage(Long userId){
        recordService.addPointsRecord(userId,10,PointsRecordType.LEARNING);
    }
}
